<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>regex_replace</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="language.modifiers.html" title="Chapter 5. Variable Modifiers">
<link rel="prev" href="language.modifier.nl2br.html" title="nl2br">
<link rel="next" href="language.modifier.replace.html" title="replace">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">regex_replace</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="language.modifier.nl2br.html">Prev</a> </td>
<th width="60%" align="center">Chapter 5. Variable Modifiers</th>
<td width="20%" align="right"> <a accesskey="n" href="language.modifier.replace.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="regex_replace">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="language.modifier.regex.replace"></a>regex_replace</h2></div></div></div>
<p>
     A regular expression search and replace on a variable. Use the
      <a class="ulink" href="http://php.net/preg_replace" target="_top">
     <code class="varname">preg_replace()</code></a> syntax from the PHP manual.
  </p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
    Although Smarty supplies this regex convenience modifier, it is usually better to apply
    regular expressions in PHP, either via custom functions or modifiers. Regular expressions
    are considered application code and are not part of presentation logic.
  </p>
</div>
<p>Parameters</p>
<div class="informaltable"><table border="1">
<colgroup>
<col align="center">
<col align="center">
<col align="center">
<col align="center">
<col>
</colgroup>
<thead><tr>
<th align="center">Parameter Position</th>
<th align="center">Type</th>
<th align="center">Required</th>
<th align="center">Default</th>
<th>Description</th>
</tr></thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">string</td>
<td align="center">Yes</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>This is the regular expression to be replaced.</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">string</td>
<td align="center">Yes</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>This is the string of text to replace with.</td>
</tr>
</tbody>
</table></div>
<div class="example">
<a name="id354785"></a><p class="title"><b>Example 5.15. regex_replace</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php

$smarty-&gt;assign('articleTitle', "Infertility unlikely to\nbe passed on, experts say.");

?&gt;

   </pre>
<p>
    Where template is:
   </p>
<pre class="programlisting">

{* replace each carriage return, tab and new line with a space *}

{$articleTitle}
{$articleTitle|regex_replace:"/[\r\t\n]/":" "}

   </pre>
<p>
    Will output:
   </p>
<pre class="screen">

Infertility unlikely to
be passed on, experts say.
Infertility unlikely to be passed on, experts say.

   </pre>
</div>
</div>
<br class="example-break"><p>
   See also <a class="link" href="language.modifier.replace.html" title="replace">
   <code class="varname">replace</code></a>
   and
   <a class="link" href="language.modifier.escape.html" title="escape"><code class="varname">escape</code></a>.
  </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="language.modifier.nl2br.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="language.modifiers.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="language.modifier.replace.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">nl2br </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> replace</td>
</tr>
</table>
</div>
</body>
</html>
